<?php

function p($array) {
    dump($array, 1, '<pre>', 0);
}

function sendMail($to, $title, $content) {

    Vendor('PHPMailer.PHPMailerAutoload');
    $mail = new PHPMailer(); //实例化
    $mail->IsSMTP(); // 启用SMTP
    $mail->Host = C('MAIL_HOST'); //smtp服务器的名称（这里以QQ邮箱为例）
    $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
    $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
    $mail->Password = C('MAIL_PASSWORD'); //邮箱密码
    $mail->From = C('MAIL_FROM'); //发件人地址（也就是你的邮箱地址）
    $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
    $mail->AddAddress($to, "尊敬的客户");
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
    $mail->CharSet = C('MAIL_CHARSET'); //设置邮件编码
    $mail->Subject = $title; //邮件主题
    $mail->Body = $content; //邮件内容
    $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
    return($mail->Send());
}

function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
    if (function_exists("mb_substr")) {
        if ($suffix)
            return mb_substr($str, $start, $length, $charset) . "...";
        else
            return mb_substr($str, $start, $length, $charset);
    }
    elseif (function_exists('iconv_substr')) {
        if ($suffix)
            return iconv_substr($str, $start, $length, $charset) . "...";
        else
            return iconv_substr($str, $start, $length, $charset);
    }
    $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef]
                  [x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
    $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
    $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("", array_slice($match[0], $start, $length));
    if ($suffix)
        return $slice . "…";
    return $slice;
}

/**
 * 公共上传函数
 * @param type $maxSize 上传大小
 * @param type $exts    上传类型
 * @param type $Path    上传路径
 */
function ajaxUploader($maxSize = 3145728, $exts = array('jpg', 'gif', 'png', 'jpeg'), $Path = './Public/UploadFiles_s/') {
    $upload = new \Think\Upload(); // 实例化上传类
    $upload->maxSize = $maxSize; // 设置附件上传大小
    $upload->exts = $exts; // 设置附件上传类型
    $upload->rootPath = $Path; // 设置附件上传根目录
    $upload->savePath = ''; // 设置附件上传（子）目录
// 上传文件 
    $info = $upload->upload();
    if (!$info) {// 上传错误提示错误信息
        $response = [
            'code' => 1,
            'success' => false
        ];
    } else {// 上传成功
        $response = [
            'code' => 0,
            'success' => true,
            'oldName' => $info['file']['name'],
            'filePath' => $info['file']['savepath'],
            'fileSize' => $info['file']['size'],
            'fileSuffixes' => $info['file']['ext'],
            'saveName' => $info['file']['savename']
        ];
    }
    die(json_encode($response));
}

/**
 * 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
 *
 * @access public
 * @param string $str 待转换字串
 *
 * @return string $str 处理后字串
 */
function makeSemiangle($str) {
    $arr = array('０' => '0', '１' => '1', '２' => '2', '３' => '3', '４' => '4',
        '５' => '5', '６' => '6', '７' => '7', '８' => '8', '９' => '9',
        'Ａ' => 'A', 'Ｂ' => 'B', 'Ｃ' => 'C', 'Ｄ' => 'D', 'Ｅ' => 'E',
        'Ｆ' => 'F', 'Ｇ' => 'G', 'Ｈ' => 'H', 'Ｉ' => 'I', 'Ｊ' => 'J',
        'Ｋ' => 'K', 'Ｌ' => 'L', 'Ｍ' => 'M', 'Ｎ' => 'N', 'Ｏ' => 'O',
        'Ｐ' => 'P', 'Ｑ' => 'Q', 'Ｒ' => 'R', 'Ｓ' => 'S', 'Ｔ' => 'T',
        'Ｕ' => 'U', 'Ｖ' => 'V', 'Ｗ' => 'W', 'Ｘ' => 'X', 'Ｙ' => 'Y',
        'Ｚ' => 'Z', 'ａ' => 'a', 'ｂ' => 'b', 'ｃ' => 'c', 'ｄ' => 'd',
        'ｅ' => 'e', 'ｆ' => 'f', 'ｇ' => 'g', 'ｈ' => 'h', 'ｉ' => 'i',
        'ｊ' => 'j', 'ｋ' => 'k', 'ｌ' => 'l', 'ｍ' => 'm', 'ｎ' => 'n',
        'ｏ' => 'o', 'ｐ' => 'p', 'ｑ' => 'q', 'ｒ' => 'r', 'ｓ' => 's',
        'ｔ' => 't', 'ｕ' => 'u', 'ｖ' => 'v', 'ｗ' => 'w', 'ｘ' => 'x',
        'ｙ' => 'y', 'ｚ' => 'z',
        '（' => '(', '）' => ')', '〔' => '[', '〕' => ']', '【' => '[',
        '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
        '‘' => '\'', '’' => '\'', '｛' => '{', '｝' => '}', '《' => '<',
        '》' => '>',
        '％' => '%', '＋' => '+', '—' => '-', '－' => '-', '～' => '-',
        '：' => ':', '。' => '.', '、' => ',', '，' => ',',
        '；' => ';', '？' => '?', '！' => '!', '…' => '-', '‖' => '|',
        '”' => '"', '·' => '`', '｜' => '|', '〃' => '"',
        '　' => ' ');
    return strtr($str, $arr);
}

/**
 * 用户资金流水日志
 * @global type $huiyuan
 * @param type $money   产生价格
 * @param type $money_class 价格类型 egg xiaoe
 * @param type $description 描述
 * @param type $user 附加用户id 如果赋值则不适用当前session用户
 * @param type $type 附加类型 默认为1 1：参与价格运算 other：不参与价格运算
 * @return type string 返回新插入数据id号
 */
function memberDetail($money, $money_class = 'gold', $description = '', $user = '', $type = '1') {
    empty($_SESSION['member_id']) && exit('请先登录！');
    $member_id = empty($user) ? $member_id = $_SESSION['member_id'] : $user;
    $member = M('member')->find($member_id);

    //余额部分
    empty($member) && exit('账户不存在');
    $balance_num = 0;
    if ($money_class == 'gold') {
        $balance_num = $type == '1' ? $member['member_gold'] + $money : $member['member_gold'];
    } else {
        $balance_num = $type == '1' ? $member['member_gold'] + $money : $member['member_gold'];
    }
    $balance_num < 0 && exit('余额数据错误');

    $data = array(
        'member_id' => $member_id,
        'member_name' => $member['member_nickname'],
        'money_class' => $money_class,
        'money' => $money,
        'description' => $description,
        'balance' => $balance_num,
        'add_time' => time()
    );
    return M('member_gold_log')->add($data);
}

?>